
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Xml;

namespace Swaf.Call
{
    /// <summary>
    /// BroadcastCall is a CompositeCall type ICall implementation.  CompositeCalls contain one or more
    /// sub-call elements.  The Broadcast call, when executed calls all sub-call elements in order and 
    /// returns an IList with one entry for each enabled defined call.  That entry will be either the result (if 
    /// called) or an exception thrown by the call.
    /// </summary>
    public class BroadcastCall : BaseCompositeCall
    {
        public BroadcastCall(string id, XmlElement initInfo)
            : base(id, initInfo)
        {
        }
        public override object call(params object[] p)
        {
            bool allSuccess = true;
            IList results = new ArrayList();
            loadInternalListIfEmpty();
            if (m_internalCalls.Count != 0)
            {
                try
                {
                    Exception callException = null;
                    foreach (internalCallMember entry in m_internalCalls)
                    {
                        if (entry.enabled)
                        {
                            bool thisSuccess = false;
                            object ret = entry.makeCall(ref thisSuccess, ref callException, p);
                            if (callException != null)
                            {
                                results.Add(callException);
                            }
                            else
                            {
                                results.Add(ret);
                            }

                            allSuccess = allSuccess && thisSuccess;
                        }
                    }
                }
                catch (Exception e)
                {
                    results.Add(e);
                }
            }
            return results;
        }
    }
}
